package ${package}.core.service.impl;


import ${package}.base.entity.${className};
import ${package}.base.request.pc.${lowerClassName}.${className}AddSTO;
import ${package}.base.request.pc.${lowerClassName}.${className}ModifySTO;
import ${package}.base.request.pc.${lowerClassName}.${className}PageSTO;
import ${package}.base.response.pc.${lowerClassName}.${className}PageDTO;
import ${package}.common.entity.B;
import ${package}.common.entity.R;
import ${package}.common.entity.P;
import ${package}.common.util.TransferUtil;
import ${package}.base.request.IdListSTO;
import ${package}.core.service.${className}Service;
import ${package}.base.mapper.${className}Mapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
* @description ${tableComment}-服务实现
* @author ${author}
* @date ${date}
* @website https://gitee.com/sunalee/fastboot-vue
**/
@AllArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {

    @Override
    public P<${className}PageDTO> page(${className}PageSTO sto){

        LambdaQueryWrapper<${className}> sql = new LambdaQueryWrapper<>();

        <#if queryColumns??>
            <#list queryColumns as column>
            <#if column.fieldQueryType == 'BETWEEN'>
        if( ObjectUtils.isNotEmpty( sto.get${column.upperColumnName}Start() ) && ObjectUtils.isNotEmpty( sto.get${column.upperColumnName}End() ) ){
                <#else>
        if( ObjectUtils.isNotEmpty( sto.get${column.upperColumnName}() ) ){
            </#if>
            <#if column.fieldQueryType == '='>
            sql.eq( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == '!='>
            sql.ne( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == '>'>
            sql.gt( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == '>='>
            sql.ge( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == '<'>
            sql.lt( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == '<='>
            sql.le( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == 'LIKE'>
            sql.like( ${className}::get${column.upperColumnName},sto.get${column.upperColumnName}() );
            </#if>
            <#if column.fieldQueryType == 'BETWEEN'>
            sql.between(${className}::get${column.upperColumnName},sto.get${column.upperColumnName}Start(),sto.get${column.upperColumnName}End());
            </#if>
        }

            </#list>
        </#if>

        <#if delFlag == true>
            <#if delFlagFieldJavaType == 'Boolean'>
        sql.eq( ${className}::getDelFlag,false );
            <#else>
        sql.eq( ${className}::getDelFlag,0 );
            </#if>
        </#if>

        Page page = baseMapper.selectPage(sto.page(),sql);
        List records = page.getRecords();
        if( ObjectUtils.isNotEmpty( records ) ){
            records = TransferUtil.changeToList(records,  ${className}PageDTO.class);
        }
        return P.autoSuccess(records,page);
    }

    @Override
    public R<${className}> findById(${pkColumnType} ${pkLowerColName}){
        return R.successd( baseMapper.selectById(${pkLowerColName}) );
    }

    @Override
    public R<${className}> add(${className}AddSTO sto){
        sto.checkParam();
        ${className} ${lowerClassName} = TransferUtil.changeToObj(sto,  ${className}.class);

        baseMapper.insert(${lowerClassName});

        return R.successd( ${lowerClassName} );
    }

    @Override
    public B modify(${className}ModifySTO sto){
        sto.checkParam();
        ${className} ${lowerClassName} = TransferUtil.changeToObj(sto,  ${className}.class);

        int r = baseMapper.updateById(${lowerClassName});

        return B.checkSuccess( r );
    }

    @Override
    public B deleteAll(IdListSTO sto){
    <#if delFlag == true>
        <#if delFlagFieldJavaType == 'Boolean'>
        int update = baseMapper.update( new ${className}().setDelFlag(true), new LambdaQueryWrapper<${className}>().in( ${className}::get${pkUpperColName}, sto.getIds() ) );
        <#else>
        int update = baseMapper.update( new ${className}().setDelFlag(1), new LambdaQueryWrapper<${className}>().in( ${className}::get${pkUpperColName}, sto.getIds() ) );
        </#if>
    <#else>
        int update = baseMapper.delete( new LambdaQueryWrapper<${className}>().in( ${className}::get${pkUpperColName}, sto.getIds() ) );
    </#if>
        return B.checkSuccess( update );
    }

}